﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Classified.Domain.Entities;
using Classified.Service;
using MvcPaging;

namespace Classified.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        private readonly ISecurityService _securityService;
        public UserController(ISecurityService securityService)
        {
            _securityService = securityService;
        }
    
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var data = _securityService.GetUsers().OrderByDescending(i => i.Id).ToPagedList(currentPageIndex, 10);
            return View(data);
        }
     
   
        // GET: /Admin/User/Create
        public ActionResult Create()
        {
            ViewBag.RolesList = _securityService.Roles();
            var user = new User { IsActive = true };
            return View(user);
        }

        // POST: /Admin/User/Create
        [HttpPost]
        public ActionResult Create(User model,List<string> role )
        {

            if (ModelState.IsValid)
            {
                if (_securityService.IsUserNameAvailabel(model.UserName))
                {
                    model.DateCreated = DateTime.Now;
                    model.DateLastPasswordChange = DateTime.Now;
                   _securityService.CreateUser(model);
                   _securityService.AssignRole(model.Id, role);
                    return RedirectToAction("List");
                }
                ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
            }
            var roleslist = new List<Role>();
            if(role!=null)
            {
                roleslist.AddRange(role.Select(r => _securityService.GetRole(r)).Where(data => data != null));
            }
            model.Roles = roleslist;
            ViewBag.RolesList = _securityService.Roles();
            return View(model);
           
        }
        public ActionResult ChangePassword(string newPassword, int userId)
        {
            var user = _securityService.GetUserById(userId);
            if (user != null)
            {
                if (newPassword.Length > 3)
                {
                    //user.Password = CryptorEngine.Encrypt(newPassword, true);
                    //_accountService.Update(user);
                    _securityService.UpdateUser(user);

                }
            }
            return RedirectToAction("List");
        }

        //
        // GET: /Admin/User/Edit/5

        public ActionResult Edit(int id)
        {
            var data = _securityService.GetUserById(id);
            if (data == null)
                return RedirectToAction("List");
            ViewBag.RolesList = _securityService.Roles();
            return View(data);
        }

        //
        // POST: /Admin/User/Edit/5

        [HttpPost]
        public ActionResult Edit(User model,List<string> role)
        {
            var roleslist = new List<Role>();
            
            if (ModelState.IsValid)
            {
                var user = _securityService.GetUserById(model.Id);
                if (user.UserName == model.UserName || _securityService.IsUserNameAvailabel(model.UserName))
                {
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.UserName = model.UserName;
                    user.IsActive = model.IsActive;
                    _securityService.UpdateUser(user);
                    if(role==null)
                        role=new List<string>();
                    _securityService.AssignRole(user.Id,role);
                   return RedirectToAction("List");
                   
                }

                if (role != null)
                {
                    roleslist.AddRange(role.Select(r => _securityService.GetRole(r)).Where(data => data != null));
                }
                model.Roles = roleslist;
                ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");

            }
            ViewBag.RolesList = _securityService.Roles();
            return View(model);
        }

        //
        // GET: /Admin/User/Delete/5

        public ActionResult Delete(int id)
        {
            _securityService.DeleteUser(id);
            return RedirectToAction("List");
        }

    }
}
